import { ref } from 'vue'
import axios from 'axios'

let num = ref(0)

//获取购物车列表
let cartApi = ref('http://localhost:3001/api/cart/list')
let cartListArr = ref([])
async function getCartList() {
  let token = localStorage.getItem('token')
  let userid = localStorage.getItem('userid')
  if (token == null) {
    num.value = 0
  } else {
    let r = await axios.post(cartApi.value, { userid }, { headers: { token } })
    if (r.data.data) {
      cartListArr.value = r.data.data
      num.value = r.data.data.length
    } else {
      cartListArr.value = []
      num.value = 0
    }
  }
}

export { num, getCartList }
